<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
        <title>1.1 Quick Start</title>
        <link rel="stylesheet" href="../css/main.css" type="text/css" media="screen" title="Ref" charset="utf-8"/>
    </head>
    <body class="body">
    Create your Grails application<p class="paragraph"/><div class="code"><pre>grails create&#45;app test&#45;freemaker&#45;prj
cd test&#45;freemaker&#45;prj</pre></div><p class="paragraph"/>
Install the plugin<p class="paragraph"/><div class="code"><pre>grails install&#45;plugin freemarker&#45;tags</pre></div><p class="paragraph"/>
Create a domain class<p class="paragraph"/><div class="code"><pre>grails create&#45;domain&#45;class MyUser</pre></div><p class="paragraph"/>
Add some fields and validations to your grails-app/domain/test/freemaker/prj/MyUser.groovy<p class="paragraph"/><div class="code"><pre><span class="java&#45;keyword">static</span> constraints = &#123;
    username(unique: <span class="java&#45;keyword">true</span>, blank: <span class="java&#45;keyword">false</span>)
    password(blank: <span class="java&#45;keyword">false</span>)
  &#125;<p class="paragraph"/>  <span class="java&#45;object">String</span> username
  <span class="java&#45;object">String</span> password</pre></div><p class="paragraph"/>
Generate a controller<p class="paragraph"/><div class="code"><pre>grails generate&#45;controller test.freemaker.prj.MyUser</pre></div><p class="paragraph"/>
Add new actions to your grails-app/controllers/test/freemaker/prj/MyUserController.groovy<p class="paragraph"/><div class="code"><pre>def login = &#123;
      def myUserInstance = <span class="java&#45;keyword">new</span> MyUser()
      render(view: <span class="java&#45;quote">"login"</span>, model: &#91;myUserInstance: myUserInstance&#93;)
    &#125;<p class="paragraph"/>    def doLogin = &#123;
      def myUserInstance = <span class="java&#45;keyword">new</span> MyUser(params)<p class="paragraph"/>      <span class="java&#45;object">boolean</span> ok = myUserInstance.validate(&#91;'password'&#93;) 
      <span class="java&#45;keyword">if</span> (ok) &#123;
	<span class="java&#45;keyword">if</span> (!myUserInstance.username || !myUserInstance.password || !myUserInstance.password.equals(<span class="java&#45;quote">"pass"</span>)) &#123;
	  flash.message = <span class="java&#45;quote">"$&#123;message(code: 'Invalid password or username', args: &#91;&#93;)&#125;"</span>
	  ok = <span class="java&#45;keyword">false</span>
	&#125;
      &#125;<p class="paragraph"/>      <span class="java&#45;keyword">if</span> (ok) &#123;
	redirect(action: <span class="java&#45;quote">"index"</span>)
      &#125; <span class="java&#45;keyword">else</span> &#123;
	myUserInstance.password = <span class="java&#45;keyword">null</span>
	render(view: <span class="java&#45;quote">"login"</span>, model: &#91;myUserInstance: myUserInstance&#93;)
      &#125;
    &#125;</pre></div><p class="paragraph"/>
Create your grails-app/views/myUser/login.ftl<p class="paragraph"/><div class="code"><pre>&#60;&#35;ftl /&#62;<p class="paragraph"/>&#60;html&#62;
  &#60;head&#62;
    &#60;meta http&#45;equiv=<span class="java&#45;quote">"Content&#45;Type"</span> content=<span class="java&#45;quote">"text/html; charset=UTF&#45;8"</span> /&#62;
    &#60;meta name=<span class="java&#45;quote">"layout"</span> content=<span class="java&#45;quote">"main"</span> /&#62;
  &#60;/head&#62;
  &#60;body&#62;
    &#60;div class=<span class="java&#45;quote">"body"</span>&#62;
      &#60;&#35;<span class="java&#45;keyword">if</span> flash.message?exists&#62;&#60;div class=<span class="java&#45;quote">"message"</span>&#62;$&#123;flash.message&#125;&#60;/div&#62;&#60;/&#35;<span class="java&#45;keyword">if</span>&#62;
      &#60;@g.hasErrors bean=myUserInstance&#62;
        &#60;div class=<span class="java&#45;quote">"errors"</span>&#62;
          &#60;@g.renderErrors bean=myUserInstance _as=<span class="java&#45;quote">"list"</span> /&#62;
        &#60;/div&#62;
      &#60;/@g.hasErrors&#62; 
      &#60;@g.form action=<span class="java&#45;quote">"doLogin"</span> method=<span class="java&#45;quote">"post"</span>&#62;
	&#60;div class=<span class="java&#45;quote">"dialog"</span>&#62;
	  &#60;table&#62;
            &#60;tr class=<span class="java&#45;quote">"prop"</span>&#62;
              &#60;td valign=<span class="java&#45;quote">"top"</span> class=<span class="java&#45;quote">"name"</span>&#62;
		&#60;label <span class="java&#45;keyword">for</span>=<span class="java&#45;quote">"username"</span>&#62;&#60;@g.message code=<span class="java&#45;quote">"myUser.username.label"</span> <span class="java&#45;keyword">default</span>=<span class="java&#45;quote">"Username"</span> /&#62;&#60;/label&#62;
              &#60;/td&#62;
              &#60;td valign=<span class="java&#45;quote">"top"</span> class=<span class="java&#45;quote">"value $&#123;g._hasErrors(&#123;'bean': myUserInstance, 'field': 'username'&#125;, 'errors')&#125;"</span>&#62;
		&#60;@g.textField name=<span class="java&#45;quote">"username"</span> value=myUserInstance.username /&#62;
            &#60;/td&#62;
            &#60;/tr&#62;	  
            &#60;tr class=<span class="java&#45;quote">"prop"</span>&#62;
              &#60;td valign=<span class="java&#45;quote">"top"</span> class=<span class="java&#45;quote">"name"</span>&#62;
		&#60;label <span class="java&#45;keyword">for</span>=<span class="java&#45;quote">"password"</span>&#62;&#60;@g.message code=<span class="java&#45;quote">"myUser.password.label"</span> <span class="java&#45;keyword">default</span>=<span class="java&#45;quote">"Password"</span> /&#62;&#60;/label&#62;
              &#60;/td&#62;
              &#60;td valign=<span class="java&#45;quote">"top"</span> class=<span class="java&#45;quote">"value $&#123;g._hasErrors(&#123;'bean': myUserInstance, 'field': 'password'&#125;, 'errors')&#125;"</span>&#62;
		&#60;@g.passwordField name=<span class="java&#45;quote">"password"</span> value=myUserInstance.password /&#62;
              &#60;/td&#62;
            &#60;/tr&#62;
	  &#60;/table&#62;
	&#60;/div&#62;
        &#60;div class=<span class="java&#45;quote">"buttons"</span>&#62;
          &#60;span class=<span class="java&#45;quote">"button"</span>&#62;&#60;@g.submitButton name=<span class="java&#45;quote">"submit"</span> value=<span class="java&#45;quote">"$&#123;g._message(&#123;'code': '<span class="java&#45;keyword">default</span>.button.login.label', '<span class="java&#45;keyword">default</span>': 'Login'&#125;)&#125;"</span> /&#62;&#60;/span&#62;
        &#60;/div&#62;
      &#60;/@g.form&#62;
    &#60;/div&#62;
  &#60;/body&#62;
&#60;/html&#62;</pre></div><p class="paragraph"/>
Use your browser to navigate to http://localhost:8080/test-freemaker-prj/myUser/login<p class="paragraph"/>Done!<p class="paragraph"/><blockquote class="note">
In FreeMarker templates, you can use &#60; &#62; or &#91; ]. For more information, please consult <a href="http://freemarker.sourceforge.net/docs/app_faq.html#faq_alternative_syntax" target="blank">Freemarker FAQ</a> about alternative syntax.
</blockquote><p class="paragraph"/><blockquote class="warning">
Be aware that FreeMarker syntax differs from GSP syntax (they are different beasts).
</blockquote>
    </body>
</html>
